import { getSearch } from '@/types/data'
import { searchAction } from '@/types/store'
import { getHistory } from '@/utils/searchOption'

/* eslint-disable camelcase */
type SearchState ={
  suggestion: string[]
  searchHistory: string[]
  getSearch: getSearch
}
const initialState: SearchState = {
  suggestion: [],
  searchHistory: getHistory() || ['javascript', 'js'], // 初始搜索关键字
  getSearch: {
    page: 1,
    per_page: 10,
    total_count: 10,
    results: []
  }
}

const profile = (state:SearchState = initialState, action: searchAction) => {
  if (action.type === 'search/option') {
    return { ...state, suggestion: action.payload }
  } else if (action.type === 'search/addHistory') { // 添加历史
    // // const temp = state.searchHistory.filter(it => it !== action.payload)
    // // setHistory([action.payload, ...temp])
    return { ...state, searchHistory: action.payload }
  } else if (action.type === 'search/getSearch') {
    return { ...state, getSearch: action.payload }
  }
  return state
}

export default profile
